﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using L.Basic.Log;
using System.Threading;

namespace System.Web.Mvc
{
    /// <summary>
    /// 异常筛选器帮助类
    /// </summary>
    internal static class ErrorFilterHelper
    {
        /// <summary>
        /// 设置异常环境上下文并显示异常页面
        /// </summary>
        /// <param name="filterContext">异常环境上下文</param>
        /// <param name="master">母板页名称</param>
        /// <param name="view">视图名称</param>
        public static void SetFilerContext(ExceptionContext filterContext, string master, string view)
        {
            string controllerName = (string)filterContext.RouteData.Values["controller"];
            string actionName = (string)filterContext.RouteData.Values["action"];
            var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
            var result = new ViewResult
            {
                ViewName = view,
                MasterName = master,
                ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
                TempData = filterContext.Controller.TempData
            };
            filterContext.Result = result;
            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.Clear();
            filterContext.HttpContext.Response.StatusCode = 200;
            filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
        }

        ///// <summary>
        ///// 记录异常日志
        ///// </summary>
        ///// <param name="filterContext">异常环境上下文</param>
        //public static void Error(ExceptionContext filterContext)
        //{
        //    ExceptionLogContent entity = new ExceptionLogContent()
        //    {
        //        Date = DateTime.Now,
        //        Message = filterContext.Exception.Message,
        //        Exception = filterContext.Exception.ToString(),
        //        Level = string.Empty,
        //        Logger = string.Empty,
        //        Thread = Thread.CurrentThread.Name
        //    };
            
        //    L.Manage.Log.Save(new ExceptionLoger() { Content = entity });
        //}
    }
}
